package com.liyao.storehouse.service.impl;

import com.liyao.storehouse.service.logService;
import com.liyao.storehouse.utils.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class logServiceImpl implements logService {

    @Override
    public JSONObject getToolReceiveLogList(int page, int limit) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        Map<String,String> map=new HashMap<>();
        map.put("curPageNo", page+"");
        map.put("pageSize", limit+"");
        String x=HttpUtil.post("http://47.103.197.129:50400/lc/log_receive/select",map);
        JSONObject list=JSONObject.fromObject(x);
        JSONArray listArr=JSONArray.fromObject(list.get("data").toString());
        System.out.println(listArr);
        for (int i = 0; i < listArr.size(); i++) {
            JSONObject jsonObject = listArr.getJSONObject(i);
            JSONObject user = jsonObject.getJSONObject("user");
            js.put("username",user.get("username").toString());
            js.put("id",jsonObject.get("id").toString());
            if (jsonObject.get("status").toString().equals("1")){
                js.put("statusText","已领");
            } else if (jsonObject.get("status").toString().equals("0")) {
                js.put("statusText","未领");
            }else {
                js.put("statusText","<span style=\"color: red;\">逾期</span>");
            }

            js.put("updatedAtStr",jsonObject.get("updatedAtStr").toString());
            js.put("createdAtStr",jsonObject.get("createdAtStr").toString());
            arr.add(js);
       }
        js.put("count", list.get("total"));
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject SearchtoolReceiveLogList(int page, int limit, String toolName, String model, String location, String userName, String status) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        Map<String,String> map=new HashMap<>();
        map.put("curPageNo", page+"");
        map.put("pageSize", limit+"");
        if (status!=null && status!=""){
            map.put("status", status);
        }
        String x=HttpUtil.post("http://47.103.197.129:50400/lc/log_receive/select",map);
        JSONObject list=JSONObject.fromObject(x);
        JSONArray listArr=JSONArray.fromObject(list.get("data").toString());
        for (int i = 0; i < listArr.size(); i++) {
            JSONObject jsonObject = listArr.getJSONObject(i);
            JSONObject user = jsonObject.getJSONObject("user");
            js.put("username",user.get("username").toString());
            js.put("id",jsonObject.get("id").toString());
            js.put("status",jsonObject.get("status").toString());
            js.put("updatedAtStr",jsonObject.get("updatedAtStr").toString());
            js.put("createdAtStr",jsonObject.get("createdAtStr").toString());
            arr.add(js);
        }
        js.put("count", list.get("total"));
        js.put("code",0);
        js.put("data",arr);
        return js;
    }
}
